using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Tools;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using System.Diagnostics; 
using TaskManager.Common;
using TaskManager.WebTask;
using TaskManager.WebVisit;
using TaskManager.temp;

namespace TaskManager
{
    public partial class Form1 : Form
    {

        private TaskControl tc = new TaskControl();
        private NetEasyControl nec = new NetEasyControl();

        private const string Blog163 = @"http://blog.163.com/";
        private List<string> lAllLinks = new List<string>();

        public Form1()
        {
            InitializeComponent();
            try
            {
                if (Environment.OSVersion.Version.Major < 6)
                {
                    //base.SendToBack();
                    //IntPtr hWndNewParent = User32.FindWindow("Progman", null);
                    //User32.SetParent(base.Handle, hWndNewParent);
                }
                else
                {
                    User32.SetWindowPos(base.Handle, 1, 0, 0, 0, 0, User32.SE_SHUTDOWN_PRIVILEGE);
                }
            }
            catch (ApplicationException exx)
            {
                MessageBox.Show(this, exx.Message, "Pin to Desktop");
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            tc.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //nec.WorkMethod();
            tmrNav.Enabled = true;
            InitLinks();
            wb.Navigate("http://reg.163.com/");
        }

        private string GetUrl()
        {

            lock (this)
            {
                if (lAllLinks.Count > 0)
                {
                    string result = lAllLinks[0];
                    lAllLinks.RemoveAt(0);
                    return result;
                }
            }
            return "";

        }

        private void InitLinks()
        {
            lAllLinks = GetAllLinkList();
        }

        private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (wb.ReadyState == WebBrowserReadyState.Complete)
            {
                if (wb.Url.ToString() == "http://reg.163.com/")
                {
                    HtmlElement name = wb.Document.All["username"];
                    HtmlElement pass = wb.Document.All["password"];
                    HtmlElement submit = wb.Document.All["loginBtn"];

                    name.SetAttribute("value", "w99xfg1@163.com");
                    pass.SetAttribute("value", "www99xfgcom");
                    submit.InvokeMember("click");
                }
                else
                {

                }

            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ManagePL mpl = new ManagePL();
            //mpl.AddGoodsPl();
            //mpl.AddGoodsBrief();
            mpl.UpdateContent();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            ManageKindLink mkl = new ManageKindLink();
            mkl.AllKindLink();
        }

        private List<string> GetAllLinkList()
        {
            Encoding _PageEncode = Encoding.GetEncoding(@"gb2312");
            RegexOptions option = (RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase);
            Stream webContent = Tools.WebUtility.GetStreamFromUrl(Blog163, 3000);
            StreamReader sr = new StreamReader(webContent, _PageEncode);
            string _PageContent = sr.ReadToEnd();
            _PageContent = _PageContent.Replace("\"", "");
            _PageContent = _PageContent.Replace("\'", "");

            string regex = @"<a\s?href=(?<url>.{0,100}?)\s?title=(?<title>.{0,50}?)>";
            string field = @"url";
            List<string> sUrlList = RegTools.GetSubList(_PageContent, regex, field, option);
            List<string> result = new List<string>();
            foreach (string url in sUrlList)
            {
                if (url.IndexOf("blog/static") > 0)
                {
                    result.Add(url);
                }
            }
            return result;
        }

        private void tmrNav_Tick(object sender, EventArgs e)
        {
            string sUrl = GetUrl();
            wb.Navigate(sUrl);
            System.Diagnostics.Trace.WriteLine(sUrl + "...OK!");
            Log.WriteLog(sUrl);
            //MessageBox.Show("OK!");
        }


        protected override bool ShowWithoutActivation
        {
            get
            {
                return true;
            }
        }

        private void Form1_Activated(object sender, EventArgs e)
        {
            if (Environment.OSVersion.Version.Major >= 6)
            {
                User32.SetWindowPos(base.Handle, 1, 0, 0, 0, 0, User32.SE_SHUTDOWN_PRIVILEGE);
            }

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            if (Environment.OSVersion.Version.Major >= 6)
            {
                User32.SetWindowPos(base.Handle, 1, 0, 0, 0, 0, User32.SE_SHUTDOWN_PRIVILEGE);
            }

        }

        private void button5_Click(object sender, EventArgs e)
        {
            ManageUser mu = new ManageUser();
            mu.GetAllUser();
            
        }
    }


    internal class User32
    {
        public const int SE_SHUTDOWN_PRIVILEGE = 0x13;

        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll")]
        public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

        [DllImport("user32.dll")]
        public static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx,
            int cy, uint uFlags);
    }
}